package com.ybl.library;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * 日期：16/6/24 11:48
 * <p/>
 * 描述：
 */
public class TradeResultStepViewIndicator extends View {
    private final String TAG_NAME = this.getClass().getSimpleName();

    private Rect mRect;

    private Drawable mBigIcon;
    private Drawable mSmallIcon;

    //定义默认的高度
    private float defaultStepIndicatorNum;
    private float mCircleRadiusBig, mCircleRadiusSmall;//圆的半径  definition circle radius
    private float mCenterX;//该View的X轴的中间位置
    private float mLeftX;
    private float mRightX;
    private float mLineHeight;//连接两点间的线的长度
    private float mLineWidth;//连接两点间线的宽度
    private float mSpace;//线与点之间的间隔

    private int mStepNum = 0;//当前有几部流程    there are currently few step
    private List<Float> mCircleCenterPointPositionList;//定义所有圆的圆心点位置的集合 definition all of circles center point list

    private OnDrawIndicatorListener mOnDrawListener;

    private Paint mLinePaint;


    /**
     * 设置监听
     *
     * @param onDrawListener
     */
    public void setOnDrawListener(OnDrawIndicatorListener onDrawListener) {
        mOnDrawListener = onDrawListener;
    }

    /**
     * get圆的半径  get circle radius
     *
     * @return
     */
    public float getCircleBigRadius() {
        return mCircleRadiusBig;
    }

    public float getCircleSmallRadius() {
        return mCircleRadiusBig;
    }


    public TradeResultStepViewIndicator(Context context) {
        this(context, null);
    }

    public TradeResultStepViewIndicator(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TradeResultStepViewIndicator(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    /**
     * init
     */
    private void init() {
        mCircleCenterPointPositionList = new ArrayList<>();//初始化

        mLinePaint = new Paint();
        mLinePaint.setAntiAlias(true);
        mLinePaint.setColor(Utils.getColor(getContext(), R.color.color_trade_result_line));
        mLinePaint.setStyle(Paint.Style.FILL);
        mLinePaint.setStrokeWidth(1);

        defaultStepIndicatorNum = Utils.getDip(getContext(), 24);
        mCircleRadiusBig = Utils.getDip(getContext(), 12);
        mCircleRadiusSmall = Utils.getDip(getContext(), 5);
        mLineHeight = Utils.getDip(getContext(), 96);
        mLineWidth = Utils.getDip(getContext(), 1);
        mSpace = Utils.getDip(getContext(), 2);

        mBigIcon = ContextCompat.getDrawable(getContext(), R.drawable.icon_trade_result_big);
        mSmallIcon = ContextCompat.getDrawable(getContext(), R.drawable.icon_trade_result_small);

    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        Log.i(TAG_NAME, "onMeasure");
        int width = (int) defaultStepIndicatorNum;
        int height = 0;
        if (mStepNum > 0) {
            //dynamic measure VerticalStepViewIndicator height
            height = (int) (getPaddingTop() + getPaddingBottom() + mCircleRadiusBig * 2 +
                    mCircleRadiusSmall * 2 * (mStepNum - 1) + (mStepNum - 1) * mLineHeight + (mStepNum - 1) * mSpace * 2);
        }
        if (MeasureSpec.UNSPECIFIED != MeasureSpec.getMode(widthMeasureSpec)) {
            width = Math.min(width, MeasureSpec.getSize(widthMeasureSpec));
        }
        setMeasuredDimension(width, height);

    }


    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        Log.i(TAG_NAME, "onSizeChanged");
        mCenterX = getWidth() / 2;
        mLeftX = mCenterX - (mLineWidth / 2);
        mRightX = mCenterX + (mLineWidth / 2);

        int paddingTop = getPaddingTop();
        for (int i = 0; i < mStepNum; i++) {
            if (i == 0) {
                mCircleCenterPointPositionList.add(paddingTop + mCircleRadiusBig);
            } else {
                mCircleCenterPointPositionList.add(paddingTop +
                        mCircleRadiusBig * 2 + (i - 1) * mCircleRadiusSmall * 2 +
                        i * (mLineHeight + mSpace * 2) + mCircleRadiusSmall);
            }
        }
        /**
         * set listener
         */
        if (mOnDrawListener != null) {
            mOnDrawListener.ondrawIndicator();
        }
    }

    @SuppressLint("DrawAllocation")
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Log.i(TAG_NAME, "onDraw");
        if (mOnDrawListener != null) {
            mOnDrawListener.ondrawIndicator();
        }
        //-----------------------画线-------draw line-----------------------------------------------
        for (int i = 0; i < mCircleCenterPointPositionList.size() - 1; i++) {
            //前一个ComplectedXPosition
            final float preComplectedXPosition = mCircleCenterPointPositionList.get(i);
            //后一个ComplectedXPosition
            final float afterComplectedXPosition = mCircleCenterPointPositionList.get(i + 1);

            if (i == 0) {
                canvas.drawRect(mLeftX, preComplectedXPosition + mCircleRadiusBig + mSpace,
                        mRightX, afterComplectedXPosition - mCircleRadiusSmall - mSpace, mLinePaint);
            } else {
                canvas.drawRect(mLeftX, preComplectedXPosition + mCircleRadiusSmall + mSpace,
                        mRightX, afterComplectedXPosition - mCircleRadiusSmall - mSpace, mLinePaint);
            }
        }
        //-----------------------画线-------draw line-----------------------------------------------

        //-----------------------画图标-----draw icon-----------------------------------------------
        for (int i = 0; i < mCircleCenterPointPositionList.size(); i++) {
            final float currentComplectedXPosition = mCircleCenterPointPositionList.get(i);
            if (i == 0) {
                mRect = new Rect((int) (mCenterX - mCircleRadiusBig), (int) (currentComplectedXPosition - mCircleRadiusBig),
                        (int) (mCenterX + mCircleRadiusBig), (int) (currentComplectedXPosition + mCircleRadiusBig));
                mBigIcon.setBounds(mRect);
                mBigIcon.draw(canvas);
            } else {
                mRect = new Rect((int) (mCenterX - mCircleRadiusSmall), (int) (currentComplectedXPosition - mCircleRadiusSmall),
                        (int) (mCenterX + mCircleRadiusSmall), (int) (currentComplectedXPosition + mCircleRadiusSmall));
                mSmallIcon.setBounds(mRect);
                mSmallIcon.draw(canvas);
            }
        }
        //-----------------------画图标-----draw icon-----------------------------------------------
    }


    /**
     * 得到所有圆点所在的位置
     *
     * @return
     */
    public List<Float> getCircleCenterPointPositionList() {
        return mCircleCenterPointPositionList;
    }

    /**
     * 设置流程步数
     *
     * @param stepNum 流程步数
     */
    public void setStepNum(int stepNum) {
        this.mStepNum = stepNum;
        requestLayout();
    }


    /**
     * 设置默认图片
     *
     * @param smallIcon
     */
    public void setSmallIcon(Drawable smallIcon) {
        this.mSmallIcon = smallIcon;
    }

    /**
     * 设置已完成图片
     *
     * @param bigIcon
     */
    public void setmBigIcon(Drawable bigIcon) {
        this.mBigIcon = bigIcon;
    }


    /**
     * 设置对view监听
     */
    public interface OnDrawIndicatorListener {
        void ondrawIndicator();
    }


}
